package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.DetailsinfoDaoimpl;
import com.ysd.dao.impl.ProductdetailsinfoDaoImpl;
import com.ysd.dao.impl.xinwenDaoImpl;
import com.ysd.entity.Detailsinfo;
import com.ysd.entity.Productdetailsinfo;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminXinWenXiangQingServlet extends BaseServlet {
	public void getxinwenxiangqing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		xinwenDaoImpl pcDao = new xinwenDaoImpl();//创建实现类对象
		List<Detailsinfo> pclist= pcDao.xinwenxiangqing();//查询所有分类
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		Gson gson = new Gson();
		String res = gson.toJson(pclist);//将pclist装华为json字符串
		out.print(res);//响应数据
		out.flush();
		out.close();
	}
	public void delxinwewn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("productclassid"));
		xinwenDaoImpl pcDao = new xinwenDaoImpl();
		int n= pcDao.delxinwen(cId);
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		if (n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		} else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		out.flush();
		out.close();
	}
	public void getAdminxinwenByWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int consultationId = 0;//产品先价格
		if(request.getParameter("consultationId")!=null && !request.getParameter("consultationId").equals("")) {
			consultationId = Integer.parseInt(request.getParameter("consultationId"));
		}
		int newsId = 0;//产品原价格
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));
		}
		String newsDetails = request.getParameter("newsDetails");//产品品牌
		String newsTitle = request.getParameter("newsTitle");//产品颜色
		String nt = request.getParameter("nt");//产品风格
		Detailsinfo p = new Detailsinfo(newsId, newsTitle, newsDetails, consultationId, nt, "");
		DetailsinfoDaoimpl pDao = new DetailsinfoDaoimpl();
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Detailsinfo> dList = pDao.getDetailsinfoByPage(p, page, size);//调用实现类中的方法	
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(dList);
		out.print(res);
		
		out.flush();
		out.close();
	}

public void getAllxinwenByWhere(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int consultationId = 0;//产品先价格
		if(request.getParameter("consultationId")!=null && !request.getParameter("consultationId").equals("")) {
			consultationId = Integer.parseInt(request.getParameter("consultationId"));
		}
		int newsId = 0;//产品原价格
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));
		}
		String newsDetails = request.getParameter("newsDetails");//产品品牌
		String newsTitle = request.getParameter("newsTitle");//产品颜色
		String nt = request.getParameter("nt");//产品风格
		Detailsinfo p = new Detailsinfo(newsId, newsTitle, newsDetails, consultationId, nt, "");
		DetailsinfoDaoimpl pDao = new DetailsinfoDaoimpl();
		List<Detailsinfo> dList = pDao.getDetailsinfo(p);//调用实现类中的方法	
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(dList.size());
		out.print(res);
		
		out.flush();
		out.close();
	}
	public void addxinwenxiangqing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		xinwenDaoImpl pcDao = new xinwenDaoImpl();
		
		String nt = request.getParameter("nt");
		String newsDetails = request.getParameter("newsDetails");
		String newsTitle = request.getParameter("newsTitle");//产品名称
		int consultationId = 0;//产品所属分类
		if(request.getParameter("consultationId")!=null && !request.getParameter("consultationId").equals("")) {
			consultationId = Integer.parseInt(request.getParameter("consultationId"));
		}	
		//接受前台传来的参数
		if (newsTitle!=null&&!newsTitle.equals("")) {
			List<Detailsinfo> pcList = pcDao.getDet(newsTitle);
			//创建实现类对象
			if (pcList.size()==0) {
				Detailsinfo pc = new Detailsinfo(0, newsTitle, newsDetails, consultationId, nt);
				//等于零的时候可以添加
				//调用构造方法，创建餐品详情的对象
				System.out.println(pc);
				//打印
				
				int n= pcDao.addxin(pc);
				// 调用实现类的构造方法执行sql语句返回受影响行数
				if (n>0) {
					out.print(Result.toClient(true, "成功",""));
					//大于0成功
				} else {
					out.print(Result.toClient(false, "删除失败",""));
					//否则失败
				}
			} else {
				out.print(Result.toClient(false, "分类名已存在",""));
			}
		}
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void geDetailsinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		DetailsinfoDaoimpl pcDao = new DetailsinfoDaoimpl();
		int cateId = Integer.parseInt(request.getParameter("cId"));
		Detailsinfo pc = pcDao.getDetailsinfoById(cateId);
		Gson gson = new Gson();
		String res = gson.toJson(pc);
		out.print(res);
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void upDetailsinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DetailsinfoDaoimpl pcDao = new DetailsinfoDaoimpl();
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		int newsId = Integer.parseInt(request.getParameter("newsId"));
		int consultationId = Integer.parseInt(request.getParameter("consultationId"));
		String newsTitle = request.getParameter("newsTitle");
		String newsDetails = request.getParameter("newsDetails");
		String nt = request.getParameter("nt");
		Detailsinfo pc = new Detailsinfo(newsId, newsTitle, newsDetails, consultationId, nt);
		int n = pcDao.updateDetailsinfo(pc);
		if (n>0) {
			out.print(Result.toClient(true, "修改成功",""));
		} else {
			out.print(Result.toClient(false, "修改失败",""));
		}
		out.flush();
		out.close();
	}

}
